package com.safeluck.scannerqcr;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.google.zxing.Result;
import com.safeluck.scannerqcr.view.MyScannerView;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import me.dm7.barcodescanner.zxing.ZXingScannerView;

/**
 * ScannerQCR
 * Created by lzw on 2017/11/23. 10:12:56
 * 邮箱：632393724@qq.com
 * All Rights Saved! Chongqing AnYun Tech co. LTD
 */
public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler, View.OnClickListener {

    private static final String TAG = MainActivity.class.getCanonicalName();
    private MyScannerView myScannerView;
    ScheduledExecutorService mExecutorService = Executors.newSingleThreadScheduledExecutor();
    private int count = 0;//计时


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myScannerView = new MyScannerView(this);

        setContentView(R.layout.activity_main);
        mExecutorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                if (count == 89){
                    mExecutorService.shutdown();

                    //时间到之后如果还没有扫描出来就关闭此界面
                    finish();

                }
                Log.i(TAG,"当前count:"+count++);
            }
        },1,1, TimeUnit.SECONDS);

        ViewGroup viewGroup =  (ViewGroup) findViewById(R.id.content_frame);
        viewGroup.addView(myScannerView);
        findViewById(R.id.tv_cancle).setOnClickListener(this);

    }


    @Override
    protected void onResume() {
        super.onResume();
        myScannerView.setAutoFocus(true);
        myScannerView.setResultHandler(this);
        myScannerView.startCamera();
    }

    @Override
    public void onPause() {
        super.onPause();
        myScannerView.stopCamera();
    }

    @Override
    public void handleResult(Result result) {
        Log.v(TAG, result.getText()); // Prints scan results
        Log.v(TAG, result.getBarcodeFormat().toString()); // Prints the scan format (qrcode, pdf417 etc.)



      //以下dialog 只有用户点击了 ok 才消失，才会扫描新的二维码并展示

            new AlertDialog.Builder(this).setMessage("message:"+result.getText()+",Format:"+result.getBarcodeFormat().toString())
                    .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.dismiss();

                            myScannerView.resumeCameraPreview(MainActivity.this);

                        }
                    }).create().show();





    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_cancle:
                finish();
                break;
        }
    }
}